// packageE/livestreaming/livestreaming.js
var app = getApp();
Component({
  properties: {
    live_search:{
      value:'',
      type:String
    }
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage: `${app.globalData.imgPath}image.png`,
    wait_page: "",

    active: 0,
    status: 3,
    //more
    isLoadMore: true,
    page: 1,
    total_page: 0,
    myConcern: "",
    announceList: "",
    broadcastlist: "",
    apiType: "AllLivelist",
    playBackData: "",
    allLivelistData: "",

    playBackShow: false,
    myConcern_activitys1: [],
    myConcern_activitys2: [],
    allLivelistData_activitys1: [],
    allLivelistData_activitys2: [],
    recommendData_activitys1: [],
    recommendData_activitys2: [],
    broadcastlist_activitys1: [],
    broadcastlist_activitys2: [],
    announceList_activitys1: [],
    announceList_activitys2: [],
    playBackData_activitys1: [],
    playBackData_activitys2: [],
    recommendData: [],
    my_colOneHeight: 0,
    my_colTwoHeight: 0,
    re_colOneHeight: 0,
    re_colTwoHeight: 0,
    br_colOneHeight: 0,
    br_colTwoHeight: 0,
    an_colOneHeight: 0,
    an_colTwoHeight: 0,
    pl_colOneHeight: 0,
    pl_colTwoHeight: 0,
    all_colOneHeight: 0,
    all_colTwoHeight: 0,
    imgPath: app.globalData.imgPath,
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.getAllLivelist();
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
   
    chunk(arr, n) {
      var result = [];
      for (var i = 0, len = arr.length; i < len; i += n) {
        result.push(arr.slice(i, i + n));
      }
      return result;
    },
    getLiveList(type) {
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-live-list"
      );
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          status: type,
          page: 1,
          keyword: this.data.live_search,
        },
        success: (resdata)=> {
          var res = resdata.data;
          if (res.result == 1) {

            if (type == 2) {
              this.setData({
                total_page: res.data.last_page,
                announceList: res.data.data,
              });
            } else if (type == 3) {
              this.setData({
                total_page: res.data.last_page,
                broadcastlist: res.data.data,
              });
            }
          } else {
            app.tips(res.msg);
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    //跳转直播页
    golivePage(e) {
      let id = e.currentTarget.dataset.id;
      let status = e.currentTarget.dataset.status;
      let memberid = e.currentTarget.dataset.memberid;
      if (status == 1) {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/livePage/livePage?id=" +
            id +
            "&playerType=live&newMedia=1",
        });
      } else {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/anchorDetail/anchorDetail?id=" + memberid,
        });
      }
    },
    //跳转直播预告
    goforeshow(e) {
      let id = e.currentTarget.dataset.id;
      wx.navigateTo({
        url: "/packageD/directSeeding/foreshow/foreshow?id=" + id,
      });
    },
    gopage(e) {
      let status = e.currentTarget.dataset.status;
      let id = e.currentTarget.dataset.id;
      let backid = e.currentTarget.dataset.backid;
      if (status == 1) {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/livePage/livePage?id=" +
            id +
            "&playerType=live&newMedia=1",
        });
      } else if (status == 2) {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/livePage/livePage?id=" +
            id +
            "&playerType=video" +
            "&backid=" +
            backid+"&newMedia=1",
        });
      }
    },
    golistPage(e) {
      let status = e.currentTarget.dataset.status;
      let id = e.currentTarget.dataset.id;
      let backid = e.currentTarget.dataset.backid;
      if (status == 3) {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/livePage/livePage?id=" +
            id +
            "&playerType=live&newMedia=1",
        });
      } else if (status == 2) {
        wx.navigateTo({
          url: "/packageD/directSeeding/foreshow/foreshow?id=" + id,
        });
      } else if (status == 4) {
        wx.navigateTo({
          url:
            "/packageD/directSeeding/livePage/livePage?id=" +
            id +
            "&playerType=video" +
            "&backid=" +
            backid+"&newMedia=1",
        });
      }
    },
    getplayBack() {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.play-back"
      );
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: 1,
          keyword: this.data.live_search,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              playBackData: res.data.data,
              total_page: res.data.last_page,
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    swichTabTItem(e) {
      let index;
      if(e) {
        index = e.detail.index;
      }else {
        index = this.data.active;
        this.data.allLivelistData_activitys1 = [];
        this.data.allLivelistData_activitys2 = [];
        this.data.recommendData_activitys1 = [];
        this.data.recommendData_activitys2 = [];
        this.data.broadcastlist_activitys1 = [];
        this.data.broadcastlist_activitys2 = [];
        this.data.announceList_activitys1 = [],
        this.data.announceList_activitys2 = [];
        this.data.playBackData_activitys1 = [];
        this.data.playBackData_activitys2 = [];
      }
       
      this.setData({
        isLoadMore: true,
        page: 1,
        total_page: 0,
        active: index,
      });
      if (index == 0) {
        this.data.apiType = "AllLivelist";
        this.getAllLivelist();
      } else if (index == 1) {
        this.data.apiType = "recommend";
        this.getRecommend();
      } else if (index == 2) {
        this.data.apiType = "liveList";
        this.data.status = 3;
        this.getLiveList(3);
      } else if (index == 3) {
        this.data.apiType = "liveList";
        this.data.status = 2;
        this.getLiveList(2);
      } else if (index == 4) {
        this.data.apiType = "playBack";
        this.getplayBack();
      }
    },
    getAllLivelist() {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-all-live-list"
      );
      console.log("走这里");
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          page: 1,
          keyword: this.data.live_search,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              allLivelistData: res.data.data,
              total_page: res.data.last_page,
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    getRecommend() {
      let that = this;
      let urlStr = app.getNetAddresss(
        "plugin.room.frontend.live-list.get-live-recommend"
      );
      app._postNetWork({
        url: urlStr,
        showToastIn: false,
        data: {
          keyword: this.data.live_search,
        },
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              recommendData: res.data,
            });
          } else {
            wx.showToast({
              icon: "none",
              title: res.msg,
              duration: 1500,
            });
          }
        },
        fail: function (res) {
          console.log(res.msg);
        },
      });
    },
    goPlayBackData(e) {
      let roomid = e.currentTarget.dataset.roomid;
      let backid = e.currentTarget.dataset.backid;
      wx.navigateTo({
        url:
          "/packageD/directSeeding/livePage/livePage?id=" +
          roomid +
          "&playerType=video" +
          "&backid=" +
          backid+"&newMedia=1",
      });
    },
    onImageError(options) {
      let index = options.target.dataset.index;
      let seltype = options.target.dataset.seltype;
      // let currentId = options.currentTarget.id;
      console.log("图片加载发生错误");
      this.onImageLoad({
        target: {
          dataset: {
            index: index,
            seltype: seltype,
          },
        },
        detail: {
          width: 2340,
          height: 1080,
        },
      });
    },
    /**
     * 图片加载函数
     * 图片加载完成后，根据图片的高度，依次往第一列和第二列中推入数据
     */
    onImageLoad(options) {
      console.log("图片加载完成");
      let index = options.target.dataset.index;
      let seltype = options.target.dataset.seltype;
      let imageWidth = options.detail.width; // 图片宽度
      let imageHeight = options.detail.height; // 图片高度
      let activityObj = null;
      let scle = imageWidth / imageHeight;
      if (seltype == "myConcern") {
        let activitys = this.data.myConcern; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.myConcern_activitys1;
        let activitys2 = this.data.myConcern_activitys2;
        if (this.data.my_colOneHeight <= this.data.my_colTwoHeight) {
          this.data.my_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.my_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          myConcern_activitys1: activitys1,
          myConcern_activitys2: activitys2,
        });
      } else if (seltype == "recommend") {
        let activitys = this.data.recommendData; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.recommendData_activitys1;
        let activitys2 = this.data.recommendData_activitys2;
        if (this.data.re_colOneHeight <= this.data.re_colTwoHeight) {
          this.data.re_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.re_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          recommendData_activitys1: activitys1,
          recommendData_activitys2: activitys2,
        });
      } else if (seltype == "AllLivelist") {
        let activitys = this.data.allLivelistData; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.allLivelistData_activitys1;
        let activitys2 = this.data.allLivelistData_activitys2;
        if (this.data.all_colOneHeight <= this.data.all_colTwoHeight) {
          this.data.all_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.all_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          allLivelistData_activitys1: activitys1,
          allLivelistData_activitys2: activitys2,
        });
      } else if (seltype == "liveList_1") {
        let activitys = this.data.broadcastlist; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.broadcastlist_activitys1;
        let activitys2 = this.data.broadcastlist_activitys2;
        if (this.data.br_colOneHeight <= this.data.br_colTwoHeight) {
          this.data.br_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.br_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          broadcastlist_activitys1: activitys1,
          broadcastlist_activitys2: activitys2,
        });
      } else if (seltype == "liveList_2") {
        let activitys = this.data.announceList; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.announceList_activitys1;
        let activitys2 = this.data.announceList_activitys2;
        if (this.data.an_colOneHeight <= this.data.an_colTwoHeight) {
          this.data.an_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.an_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          announceList_activitys1: activitys1,
          announceList_activitys2: activitys2,
        });
      } else if (seltype == "playBack") {
        let activitys = this.data.playBackData; // 获取所有的数据资源
        activityObj = activitys[index];
        let activitys1 = this.data.playBackData_activitys1;
        let activitys2 = this.data.playBackData_activitys2;
        if (this.data.pl_colOneHeight <= this.data.pl_colTwoHeight) {
          this.data.pl_colOneHeight += 176 / scle + 60;
          activitys1.push(activityObj);
        } else {
          this.data.pl_colTwoHeight += 176 / scle + 60;
          activitys2.push(activityObj);
        }
        this.setData({
          playBackData_activitys1: activitys1,
          playBackData_activitys2: activitys2,
        });
      }
    },
    //获取更多数据
    _getMoreData() {
      const that = this;
      let json = {};
      let api = "";
      let urlStr = "";
      that.data.isLoadMore = false; // 防止多次请求分页数据
      if (this.data.page >= this.data.total_page) {
        return;
      } else {
        this.data.page = this.data.page + 1;
        if (this.data.apiType == "myConcern") {
          api = "plugin.room.frontend.live-list.get-live-concern";
          json = {
            page: this.data.page,
          };
        } else if (that.data.apiType == "AllLivelist") {
          api = "plugin.room.frontend.live-list.get-all-live-list";
          json = {
            page: this.data.page,
          };
        } else if (that.data.apiType == "playBack") {
          api = "plugin.room.frontend.live-list.play-back";
          json = {
            page: this.data.page,
          };
        }
        //  else if (that.data.apiType == "recommend") {
        // }
        else {
          api = "plugin.room.frontend.live-list.get-live-list";
          json = {
            status: this.data.status,
            page: this.data.page,
          };
        }
        json.keyword = this.data.live_search;
        urlStr = app.getNetAddresss(api);
        app._postNetWork({
          url: urlStr,
          showToastIn: false,
          data: json,
          success: function (resdata) {
            var res = resdata.data;
            if (res.result == 1) {
              if (that.data.apiType == "myConcern") {
                that.setData({
                  isLoadMore: true,
                  myConcern: that.data.myConcern.concat(res.data.data),
                });
              } else if (that.data.apiType == "playBack") {
                that.setData({
                  isLoadMore: true,
                  playBackData: that.data.playBackData.concat(res.data.data),
                });
              } else if (that.data.apiType == "AllLivelist") {
                that.setData({
                  isLoadMore: true,
                  allLivelistData: that.data.allLivelistData.concat(
                    res.data.data
                  ),
                });
              }
              // else if (that.data.apiType == "recommend") {
              // }
              else {
                if (that.data.status == 2) {
                  that.setData({
                    isLoadMore: true,
                    announceList: that.data.announceList.concat(res.data.data),
                  });
                } else if (that.data.status == 3) {
                  that.setData({
                    isLoadMore: true,
                    broadcastlist: that.data.broadcastlist.concat(
                      res.data.data
                    ),
                  });
                }
              }
            } else {
              that.setData({
                page: that.data.page - 1,
                isLoadMore: false,
              });
            }
          },
          fail: function (res) {
            console.log(res.msg);
          },
        });
      }
    },
  },
});
